package com.gridmobi.xrtb.model.rtb;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 增量上报统计值 如果上报异常不成功 将delta 清空 set(0)
 */

public class Accumulator {
    public static AtomicLong requests = new AtomicLong(0);
    public static AtomicLong errors = new AtomicLong(0);

    public AtomicLong bids = new AtomicLong(0);
    public AtomicLong wins = new AtomicLong(0);
    public AtomicLong imps = new AtomicLong(0);
    public AtomicLong clicks = new AtomicLong(0);
    private static long deltaRequests = 0;
    private static long deltaErrors = 0;
    private long deltaBids = 0;
    private long deltaWins = 0;
    private long deltaImps = 0;
    private long deltaClicks = 0;


    private static long requestsSnapshot = 0;
    private static long errorsSnapshot = 0;
    private long bidsSnapshot = 0;
    private long winsSnapshot = 0;
    private long impsSnapshot = 0;
    private long clicksSnapshot = 0;


    String cid;

    public Accumulator(String cid) {
        this.cid = cid;
    }

    /**
     * 计算增量 每个周期调用一次  不要重复调用 report时候调用
     */
    public synchronized  void  delta() {
        long dbids = bids.longValue();
        long dwins = wins.longValue();
        long dclicks = clicks.longValue();
        long dimps = imps.longValue();

        deltaBids = dbids - bidsSnapshot;
        deltaWins = dwins - winsSnapshot;
        deltaImps = dimps - impsSnapshot;
        deltaClicks = dclicks - clicksSnapshot;


        bidsSnapshot = dbids;
        winsSnapshot = dwins;
        clicksSnapshot = dclicks;
        impsSnapshot = dimps;


    }

    public synchronized static void deltaGlobal() {

        long drequests = requests.longValue();
        long derrors = errors.longValue();

        deltaRequests = drequests - requestsSnapshot;
        deltaErrors = derrors - errorsSnapshot;

        errorsSnapshot = derrors;
        requestsSnapshot = drequests;

    }

    public static long getDeltaRequests() {
        return deltaRequests;
    }

    public long getDeltaBids() {
        return deltaBids;
    }

    public long getDeltaWins() {
        return deltaWins;
    }

    public long getDeltaImps() {
        return deltaImps;
    }

    public long getDeltaClicks() {
        return deltaClicks;
    }

    public static long getDeltaErrors() {
        return deltaErrors;
    }


    public static void rollbackDeltaRequests() {
        Accumulator.requestsSnapshot = requestsSnapshot - deltaRequests;
    }

    public static void rollbackDeltaErrors() {
        Accumulator.errorsSnapshot = errorsSnapshot - deltaErrors;
    }

    public void rollbackDeltaBids() {
        this.bidsSnapshot = this.bidsSnapshot - this.deltaBids;
    }

    public void rollbackDeltaWins() {
        this.winsSnapshot = this.winsSnapshot - this.deltaWins;
    }

    public void rollbackDeltaImps() {
        this.impsSnapshot = this.impsSnapshot - this.deltaImps;
    }

    public void rollbackDeltaClicks() {
        this.clicksSnapshot = this.clicksSnapshot - this.deltaClicks;
    }


    public AtomicLong getRequests() {
        return requests;
    }

    public void setRequests(AtomicLong requests) {
        Accumulator.requests = requests;
    }

    public AtomicLong getErrors() {
        return errors;
    }

    public void setErrors(AtomicLong errors) {
        Accumulator.errors = errors;
    }
}
